package com.bw.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bw.bean.DataBean;
import com.bw.sercise.Datasercise;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 徐金浩
 * @date 2025年10月11日 9:33
 */
@Slf4j
@RestController
public class Datacontroller {

    @Autowired
    private Datasercise datasercise;

    @RequestMapping("/s1")
    public JSONObject s1()
    {
        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");

        JSONObject data = new JSONObject();
        result.put("data",data);

        JSONArray categories = new JSONArray();
        JSONArray series = new JSONArray();
        data.put("categories",categories);
        data.put("series",series);

        JSONObject row = new JSONObject();
        series.add(row);
        row.put("name","交易额");

        JSONArray roes = new JSONArray();
        row.put("data",roes);

        List<DataBean> beans = datasercise.select1();
        for (DataBean bean : beans) {
            categories.add(bean.getName());
            roes.add(bean.getCta());
        }
        return result;
    }

    @RequestMapping("/s2")
    public JSONObject s2()
    {
        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");

        JSONArray data = new JSONArray();
        result.put("data",data);

        List<DataBean> beans = datasercise.select2();
        for (DataBean bean : beans) {
            JSONObject row = new JSONObject();
            row.put("name",bean.getAge());
            row.put("value",bean.getCta());

            data.add(row);
        }

        return result;
    }


    @RequestMapping("/s3")
    public JSONObject s3()
    {
        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");

        JSONObject data = new JSONObject();
        result.put("data",data);

        JSONArray mapData = new JSONArray();
        JSONArray tooltipNames = new JSONArray();
        JSONArray tooltipUnits = new JSONArray();

        data.put("mapData",mapData);
        data.put("tooltipNames",tooltipNames);
        data.put("tooltipUnits",tooltipUnits);

        List<DataBean> beans = datasercise.select3();
        for (DataBean bean : beans) {
            JSONObject row = new JSONObject();
            row.put("name",bean.getName());
            row.put("value",bean.getCta());

            mapData.add(row);
        }

        return result;
    }

    @RequestMapping("/s4")
    public JSONObject s4()
    {
        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");

        JSONObject data = new JSONObject();
        result.put("data",data);

        JSONArray columns = new JSONArray();
        JSONArray rows = new JSONArray();
        data.put("columns",columns);
        data.put("rows",rows);

        JSONObject c1 = new JSONObject();
        c1.put("name","品牌");
        c1.put("id","name");
        JSONObject c2 = new JSONObject();
        c2.put("name","性别");
        c2.put("id","gender");
        JSONObject c3 = new JSONObject();
        c3.put("name","交易额");
        c3.put("id","cta");


        columns.add(c1);
        columns.add(c2);
        columns.add(c3);




        List<DataBean> beans = datasercise.select4();
        for (DataBean bean : beans) {
            JSONObject row = new JSONObject();
            row.put("name",bean.getName());
            row.put("gender",bean.getGender());
            row.put("cta",bean.getCta());

            rows.add(row);
        }

        return result;
    }
}
